package com.qf.controller;

import com.alibaba.druid.util.StringUtils;
import com.qf.pojo.Address;
import com.qf.pojo.User;
import com.qf.service.AddressService;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/10/1 20:37
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    AddressService addressService;

    @RequestMapping("/findall")
    public List<User> findall(){
        List<User> findall = userService.findall();
        return findall;
    }


    @RequestMapping("/login")
    public ModelAndView login(HttpServletRequest request,ModelAndView view, HttpServletResponse response) throws UnsupportedEncodingException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String auto = request.getParameter("auto");
        User user = userService.findByUserAndPwd(username, password);
        if (user==null){
            view.setViewName("redirect:/login.jsp");
            return view;
        }else {
            //保存登录状态
            request.getSession().setAttribute("user",user);
            if ("on".equalsIgnoreCase(auto)){
                String encode = URLEncoder.encode(username + "#" + password,"utf-8");
                Cookie cookie = new Cookie("user",encode);
                cookie.setDomain("localhost");//设置域名
                cookie.setPath("/");
                cookie.setMaxAge(14*24*60*60);
                response.addCookie(cookie);
            }
            view.setViewName("redirect:/index.jsp");
            return view;
        }
    }

    @PostMapping("/register")
    public ModelAndView register(ModelAndView view , User user,HttpServletRequest request){
        user.setFlag(1);
        user.setRole(1);
        boolean register = userService.register(user);
        if (register){
             view.setViewName("redirect:/login.jsp");
        }else {
             view.setViewName("redirect:/register.jsp");
        }
        return view;
    }

    @GetMapping("/checkUserName")
    public String checkUserName(HttpServletRequest req)  {
        String username = req.getParameter("username");
        if (StringUtils.isEmpty(username)){
            return "0";
        }else {
            User user = userService.findByUsername(username);
            if (user==null){
                return "2";
            }else {
                return "1";
            }
        }
    }

    @GetMapping("/checkEmail")
    public String checkEmail(String email)  {
        if (StringUtils.isEmpty(email)){
            return "0";
        }else {
            User user = userService.findByEmail(email);
            if (user==null){
                return"2";
            }else {
                return "1";
            }
        }
    }

    @GetMapping("/checkCode")
    public String checkCode(HttpSession session,String code){
        String captcha = (String) session.getAttribute("captcha");
        if (code==null){
            return "1";
        }else if (code.equalsIgnoreCase(captcha)){
            return "0";
        }else {
            return "1";
        }
    }

    @RequestMapping("/logOut")
    public ModelAndView logOut(HttpSession session,ModelAndView view){
        session.invalidate();
        view.setViewName("redirect:/login.jsp");
        return view;
    }

    @RequestMapping("/getAddress")
    public ModelAndView getAddress(HttpSession session,ModelAndView mav){
        User user = (User) session.getAttribute("user");
        if (user==null){
            mav.setViewName("redirect:/login.jsp");
            return mav;
        }
        List<Address> addressList = addressService.findById(user.getId());
        mav.addObject("addList",addressList);
        mav.setViewName("forward:/self_info.jsp");
        return mav;
    }
}
